import { Component, ViewChild, Input } from '@angular/core';
import { Events, AlertController, App, ModalController, NavController, Content, ActionSheetController, LoadingController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { Config } from '../../../providers/config';
import { DomSanitizer } from '@angular/platform-browser';
import _ from 'lodash';

import { Common } from "../../../providers/common";
import { BcwHomeService } from "./home.service";
import { BcwHomeListPage } from "./list.component";

@Component({
  selector: 'page-bcw-home',
  templateUrl: './home.html',
  providers: [BcwHomeService]
})
export class BcwHomePage {
  moa_page_name: any = "page-bcw-home";
  moa_page_explain: any = "风控首页";

  @Input() public isFromMainPage: boolean;

  title = {
    level: "",
    fund: "",
    rule: "",
    month: ""
  };

  l_days = {
    level: "today",
    fund: "today",
    rule: "today"
  };

  ruleLevel = {
    fund: "5",
    rule: "5"
  };

  noMoreData = {
    level: false,
    fund: false,
    rule: false,
    month: false
  };

  returnData = null;

  dataList = {
    level: {
      datas: []
    },
    fund: {
      datas: []
    },
    rule: {
      datas: []
    },
    month: {
      monthDatas: [],
      dailyDatas: []
    }
  };

  constructor(
    public alertCtrl: AlertController,
    public app: App,
    public modalCtrl: ModalController,
    public navCtrl: NavController,
    public config: Config,
    private sanitizer: DomSanitizer,
    public events: Events,
    public storage: Storage,
    private common: Common,
    private actionSheetCtrl: ActionSheetController,
    private bcwHomeService: BcwHomeService,
    private loadingCtrl: LoadingController
  ) {

  }

  ngAfterViewInit() {
    if (this.isFromMainPage) {
      this.loadDatas();
    }
  }

  ionViewDidEnter() {
    if (!this.isFromMainPage) {
      this.loadDatas();
    }
  }

  loadDatas() {
    this.bcwHomeService.getBCWHomePageGridData().subscribe(returnData => {
      console.log("getBCWHomePageGridData().returnData", returnData);
      if (returnData) {
        this.returnData = returnData;
        this.initGridView("level");
        this.initGridView("fund");
        this.initGridView("rule");
        this.initGridView("month");

      } else {
        this.noMoreData.level = true;
        this.noMoreData.fund = true;
        this.noMoreData.rule = true;
        this.noMoreData.month = true;
      }
    });
  }

  initGridView(type) {
    let that = this;

    switch (type) {
      case "level":
        if (this.returnData.level) {
          this.dataList.level = this.returnData.level[this.l_days.level];
          if (this.dataList.level["datas"].length <= 0) {
            this.noMoreData.level = true;
          } else {
            this.noMoreData.level = false;

            this.dataList.level["highchart_config"].plotOptions.pie.point.events.click = function () {
              if (that.l_days.level != "today") {
                // that.common.alertMessage("只允许查看今日的预警消息详情");
                return false;
              }
              let vc_memo = "0";
              if (this.name.indexOf("普通") > -1) {
                vc_memo = "1";
              }
              if (this.name.indexOf("一般") > -1) {
                vc_memo = "2";
              }
              if (this.name.indexOf("较急") > -1) {
                vc_memo = "3";
              }
              if (this.name.indexOf("紧急") > -1) {
                vc_memo = "4";
              }
              if (this.name.indexOf("特急") > -1) {
                vc_memo = "5";
              }
              let l_days = "0";
              if (that.l_days.level === "today") {
                l_days = "0";
              } else {
                if (that.l_days.level === "week") {
                  l_days = "7";
                } else {
                  if (that.l_days.level === "month") {
                    l_days = "30";
                  } else {
                    if (that.l_days.level === "year") {
                      l_days = "365";
                    }
                  }
                }
              }
              return false;
              /*that.navCtrl.push(BcwHomeListPage, {
                l_days: l_days,
                vc_memo: vc_memo
              });*/
            };

            this.dataList.level["highchart_config"].plotOptions.pie.events.legendItemClick = function () {
              return false;
            };

            let elements = document.getElementsByClassName("BcwGrid_Level");
            _.forEach(elements, el => {
              this.dataList.level["highchart_config"].chart.renderTo = el;
              window["Highcharts"].chart(this.dataList.level["highchart_config"]);
            });
          }
        }
        break;
      case "fund":
        if (this.returnData.fund) {
          this.dataList.fund = this.returnData.fund[this.l_days.fund][this.ruleLevel.fund];
          if (this.dataList.fund.datas.length <= 0) {
            this.noMoreData.fund = true;
          } else {
            this.noMoreData.fund = false;

            this.dataList.fund["highchart_config"].plotOptions.column.events.legendItemClick = function () {
              return false;
            };

            let elements = document.getElementsByClassName("BcwGrid_Fund");
            _.forEach(elements, el => {
              this.dataList.fund["highchart_config"].chart.renderTo = el;
              window["Highcharts"].chart(this.dataList.fund["highchart_config"]);
            });
          }
        }
        break;
      case "rule":
        if (this.returnData.rule) {
          this.dataList.rule = this.returnData.rule[this.l_days.rule][this.ruleLevel.rule];
          if (this.dataList.rule.datas.length <= 0) {
            this.noMoreData.rule = true;
          } else {
            this.noMoreData.rule = false;

            this.dataList.rule["highchart_config"].plotOptions.column.events.legendItemClick = function () {
              return false;
            };

            let elements = document.getElementsByClassName("BcwGrid_Rule");
            _.forEach(elements, el => {
              this.dataList.rule["highchart_config"].chart.renderTo = el;
              window["Highcharts"].chart(this.dataList.rule["highchart_config"]);
            });
          }
        }
        break;
      case "month":
        if (this.returnData.month) {
          this.dataList.month = this.returnData.month;
          if (this.dataList.month.monthDatas.length <= 0 && this.dataList.month.dailyDatas.length <= 0) {
            this.noMoreData.month = true;
          } else {
            this.noMoreData.month = false;

            this.dataList.month["highchart_config_month"].plotOptions.column.events.legendItemClick = function () {
              return false;
            };

            let elements = document.getElementsByClassName("BcwGrid_month");
            _.forEach(elements, el => {
              this.dataList.month["highchart_config_month"].chart.renderTo = el;
              window["Highcharts"].chart(this.dataList.month["highchart_config_month"]);
            });

            this.dataList.month["highchart_config_daily"].tooltip.pointFormatter = function () {
              console.log(this);
              return that.dataList.month["dailyPoint"][this.category];
            };

            this.dataList.month["highchart_config_daily"].plotOptions.column.events.legendItemClick = function () {
              return false;
            };

            elements = document.getElementsByClassName("BcwGrid_daily");
            _.forEach(elements, el => {
              this.dataList.month["highchart_config_daily"].chart.renderTo = el;
              window["Highcharts"].chart(this.dataList.month["highchart_config_daily"]);
            });
          }
        }
        break;
    }
  }

}
